<template>

  <el-dialog :visible.sync='visible' title="请选择要转移到的分类" width='700px'
             @open="openDialog"
             @close='closeDialog'>

    <el-row>
      <el-col>
        <el-cascader :options="subTagTree" :props="tagCascadeProps"
                     v-model="selectedTagId"
                     filterable style="width: 100%"/>
      </el-col>
    </el-row>

    <div class="dialog-footer" slot="footer">
      <el-button @click='visible = false'>取 消</el-button>
      <el-button @click="submit" type="primary">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

  export default {
    name: 'TransferKnowledgeDialog',
    props: ['subTagTree', 'selectedSubTag'],
    data() {
      return {
        visible: false,
        selectedTagId: [],
        selectedTagIdOri: [],
        tagCascadeProps: {
          label: 'name',
          value: 'id',
          multiple: false,
          checkStrictly: true,
        },
      }
    },
    methods: {
      submit(){
        let param = new URLSearchParams()
        param.append("selectedTagIdOri", JSON.stringify(this.selectedTagIdOri))
        param.append("selectedTagId", JSON.stringify(this.selectedTagId))
        this.axios.post('/pmph/action/tag/transferKnowledge', param).then(() => {
          this.$message.success("知识迁移成功, 相关知识推送任务已提交, 请稍后查看推送状态")
          this.closeDialog()
        })
      },
      openDialog(){
        this.selectedTagIdOri = []
        this.selectedTagIdOri.push(this.selectedSubTag.id)
        let cntSubTag = this.selectedSubTag
        while(cntSubTag.level > 2) {
          this.selectedTagIdOri.unshift(cntSubTag.parentId)
          cntSubTag = UTIL.findTreeNode(this.subTagTree, item => item.id === cntSubTag.parentId)
        }
        this.selectedTagId = JSON.parse(JSON.stringify(this.selectedTagIdOri))
      },
      closeDialog(){
        this.visible = false
      }
    },
  }
</script>

<style scoped lang="scss">
  /deep/ .el-dialog__body {
    padding: 0px 20px;
  }
</style>
